#include "voice.h"
#include "queue.h"
#include "define.h"
#include "disp.h"
#include "power.h"
#include "clk.h"
#include "gpio.h"
#include "time.h"
#include "uart.h"
#include "spk.h"
#include "flash.h"
#include "adc.h"
#include "algo.h"
#include "bp.h"
#include "func.h"
#include "op.h"
#include "pwm.h"

char g_flag_voice_on = 0;
void delay_100us(void)
{
	u8  time_count = 135;
    while (time_count)
    {
        time_count--;
		nop4();
		//nop4();
    }
}

void delay_1ms(void)
{
    u8  i;
    for (i = 0; i < 10; i++)
    {
        delay_100us();
    }
}

void delay_500us(void)
{
    u8  i;
    for (i = 0; i < 5; i++)
    {
        delay_100us();
    }
}

//死延时 协议帧的开始信号
void voice_start(void)
{
    RST_L;
    DATA_L;
    RST_H;
    delay_100us();
    RST_L;
	delay_500us();
}

//占用资源死循环发送一个数据帧
void voice_send_cmd(u8 e_voice_cmd)
{
    u8  i;
    voice_start();
    for (i = 0; i < e_voice_cmd; i++)
    {
        DATA_H;
        delay_100us();
        DATA_L;
        delay_100us();
    }
	i = 6;
	while (Busy==1 && i > 0) //等待播放 Busy拉低
	{
		delay_500us(); //等待播放 Busy拉低
		i--;
	}

}

//语音初始化
void voice_init(void)
{
    RST_L;
    DATA_L;
    Queue_Init(&qVoice, &voice_buffer[0], Q_VOICE_BUFFER_SIZE);
}

//新协议  简单协议语音ic 发送单个cmd服务函数
void new_cn_ic_voice_send_cmd_server(unsigned char e_voice_cmd)
{
    voice_send_cmd(e_voice_cmd);
    do
    {
        if (!KEY_POWER || !KEY_MEMORYUP || !KEY_MEMORYDOWN)
        {
            VoiceStop = 1;
            break;
        }
        if ((!MemoryMode || KeyNumber == 2) && !KEY_TIME)
        {
            VoiceStop = 1;
            break;
        }
        if (!ThreeKeyFlag && !KEY_TIME)
        {
            VoiceStop = 1;
            break;
        }
        if (KeyNumber == 7 && !KEY_USER && MemoryMode)
        {
            VoiceStop = 1;
            break;
        }
    } while (!Busy);
    if (VoiceStop)  //2022年12月20日 按键按下时增加停止命令  记忆界面播报不能打断bug解决
    {
        voice_send_cmd(VOICE_CMD_NO_VOICE_STOP_VOICE);
    }
}

//新协议  简单协议语音ic 发送组合cmd服务函数
void new_cn_ic_voice_send_combine_cmd_server(void)
{
    u8 voice_temp = 0; //取队列数据

    while (g_flag_voice_on == 1)
    {
        if (Busy == 1)
        {
            if (Queue_Count(&qVoice) > 0)
            {
                if (Queue_Pop(&qVoice, &voice_temp) == QUEUE_OK)
                {
                    new_cn_ic_voice_send_cmd_server(voice_temp);
                    if (VoiceStop)
                    {
                        Queue_Clear(&qVoice);
                        g_flag_voice_on = 0;
                        break;
                    }
                }
            }
            else
            {
                Queue_Clear(&qVoice);
                g_flag_voice_on = 0;
                break;
            }
        }
    }
}

//语音处理分割数字百十个位
// e_pres_unit 选择当前分割的数字类型 e_pres_unit_typedef 中定义
// number 要进行分割的数字
void voice_devide_number_pro(e_pres_unit_typedef e_pres_unit, u16 number)
{
    u8  temp_hundred = 0;
    u8  temp_ten = 0;
    u8  temp_single = 0;
    temp_hundred = number / 100;
    temp_ten = number % 100 / 10;
    temp_single = number % 10;
    if (e_pres_unit == PRES_UNIT_MMG || e_pres_unit == PRES_UNIT_HEARTBEAT) // mmg和心跳次数处理
    {
        if (temp_hundred)
        {
            temp_hundred += VOICE_CMD_NUMBER_OFFSET;
            Queue_Push(&qVoice, temp_hundred);      //入列
            Queue_Push(&qVoice, VOICE_CMD_HUNDRED); //入列
        }
        if (temp_ten)
        {
            temp_ten += VOICE_CMD_NUMBER_OFFSET;
            Queue_Push(&qVoice, temp_ten);      //入列
            Queue_Push(&qVoice, VOICE_CMD_TEN); //入列
        }
        else //十位没有
        {
            if (temp_hundred && temp_single) //如果百位和个位都有效 十位需要加一个"零"
            {
                Queue_Push(&qVoice, 0 + VOICE_CMD_NUMBER_OFFSET); //入列
            }
        }

        if (temp_single)
        {
            temp_single += VOICE_CMD_NUMBER_OFFSET;
            Queue_Push(&qVoice, temp_single); //入列
        }
        else if (e_pres_unit == PRES_UNIT_HEARTBEAT && (!temp_hundred && !temp_ten && !temp_single)) //心跳显示HI和LOW时传入的值为0, 此时播报"零次"
        {
            Queue_Push(&qVoice, 0 + VOICE_CMD_NUMBER_OFFSET); //入列
        }

        if (e_pres_unit == PRES_UNIT_MMG) // mmg和心跳次数处理
        {
            Queue_Push(&qVoice, VOICE_CMD_MM_HG); //入列
        }
        else if (e_pres_unit == PRES_UNIT_HEARTBEAT) // mmg和心跳次数处理
        {
            Queue_Push(&qVoice, VOICE_CMD_TIME); //入列
        }
    }
    else // kpa处理
    {
        if (temp_hundred) //实际上是十位 10倍数
        {
            if (temp_hundred == 1) //为1时 只播报"十"
            {
                Queue_Push(&qVoice, VOICE_CMD_TEN); //入列
            }
            else
            {
                temp_hundred += VOICE_CMD_NUMBER_OFFSET;
                Queue_Push(&qVoice, temp_hundred);  //入列
                Queue_Push(&qVoice, VOICE_CMD_TEN); //入列
            }
        }
        if (temp_ten) //实际上是个位 10倍数
        {
            temp_ten += VOICE_CMD_NUMBER_OFFSET;
            Queue_Push(&qVoice, temp_ten); //入列
        }

        if (temp_single) //实际上是小数位 10倍数
        {
            Queue_Push(&qVoice, VOICE_CMD_SPOT); //如果小数位有效 需要加一个"点"
            temp_single += VOICE_CMD_NUMBER_OFFSET;
            Queue_Push(&qVoice, temp_single); //入列
        }
        Queue_Push(&qVoice, VOICE_CMD_KILOPASCAL); //入列
    }
}

//高压是+SYS+毫米汞柱/kpa+低压是+DIA+毫米汞柱/kpa+每分钟心跳次数是+PUL+次
//组合语音处理函数
// e_pres_unit 值:     e_pres_unit_typedef 中定义
// sys 值:     血压高压 0~255 只需输入mmg \kpa由函数内部自动转换
// dia 值:     血压低压 0~255
// pul 值:     每分钟心跳 0~255
void voice_combine_cmd(e_pres_unit_typedef e_pres_unit, u8 sys, u8 dia, u8 pul)
{
    u16  temp_sys = 0;
    u16  temp_dia = 0;
    temp_sys = sys;
    temp_dia = dia;
    if (e_pres_unit == PRES_UNIT_KPA) // mmg -> kpa转换
    {
        temp_sys *= 4;
        temp_sys /= 3;
        temp_dia *= 4;
        temp_dia /= 3;
    }

    Queue_Push(&qVoice, VOICE_CMD_HIGH_PRESSURE_IS); //入列
    voice_devide_number_pro(e_pres_unit, temp_sys);

    Queue_Push(&qVoice, VOICE_CMD_LOW_PRESSURE_IS); //入列
    voice_devide_number_pro(e_pres_unit, temp_dia);

    Queue_Push(&qVoice, VOICE_CMD_HEARTBEATS_PER_MINUTE_IS); //入列
    voice_devide_number_pro(PRES_UNIT_HEARTBEAT, pul);
}

//您的血压正常/偏高/偏低/
// wholevel 值: who的等级
void voice_combine_cmd_wholevel(u8 wholevel)
{
    voice_add_cmd(VOICE_CMD_YOUR_BLOOD_PRESSURE); //增加插入一条语音 "您的血压"
    if (wholevel <= 2)//根据who等级选择增加一条语音 "正常" "
    {
        voice_add_cmd(VOICE_CMD_NORMAL);
    }
    else
    {
        voice_add_cmd(VOICE_CMD_ON_THE_HIGH_SIDE);
    }
}

//往语音队列中添加一个指令
void voice_add_cmd(u8 e_voice_cmd)
{
    Queue_Push(&qVoice, e_voice_cmd); //入列
}

//语音开始播放
void voice_combine_cmd_start_play(void)
{
    g_flag_voice_on = 1;
}

//语音停止播放 同时清理队列 在写队列前调用
void voice_stop_play_clear_queue(void)
{
    Queue_Clear(&qVoice);
    g_flag_voice_on = 0;
    voice_send_cmd(VOICE_CMD_NO_VOICE_STOP_VOICE);
}

//lx写
void voicezh_combine_cmd(e_pres_unit1_typedef e_pres_unit1, u8 number1, u8 number2, u8 number3)
{
	e_pres_unit_typedef e_pres_time;
	voice_stop_play_clear_queue();
	if (e_pres_unit1 == _kp)
	{
	   e_pres_time=PRES_UNIT_KPA;
		voice_combine_cmd(e_pres_time, number1, number2, number3);
	}
	if (e_pres_unit1 == _mmg)
	{
	   e_pres_time=PRES_UNIT_MMG;
		voice_combine_cmd(e_pres_time, number1, number2, number3);
	}
	voice_combine_cmd_start_play(); //开始播放
	new_cn_ic_voice_send_combine_cmd_server();
}
